package com.yc.ServerClient;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/***
 * 这是一个telnet的客户端 用于访问baidu
 *  telnet www.baidu.com 80
 *  再输入
 *  GET /HTTP1.0
 */
public class Test7_telent_baidu {
    public static void main(String[] args) {
        String website="www.baidu.com";
        int port=80;

        //String http="GET /HTTP1.0\r\nHost:www.baidu.com\r\n\r\n"
        String http="GET / HTTP/1.0\r\n\r\n";  //应用层协议

        //http服务器  -》 http协议
        try(Socket s=new Socket(website,port);
            OutputStream oss=s.getOutputStream();
            InputStream iis=s.getInputStream()
        ){
            oss.write(http.getBytes());
            oss.flush();
            ByteArrayOutputStream baos=new ByteArrayOutputStream( );
            byte[]bs=new byte[10 * 1024];
            int length=-1;
            while (  (length=iis.read( bs,0,bs.length )) !=-1 ){
                //先把此次读取到的数据通过baos存到   内存中
                baos.write(bs,0,length);
            }
            baos.flush();

            byte [] bb=baos.toByteArray();
            String str=new String(bb);
            System.out.println(str);

        }catch (Exception ex){
            ex.printStackTrace();
        }


    }
}
